package com.itheima.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.domain.ProductCategory;
import com.itheima.mapper.ProductCategoryMapper;
import com.itheima.service.IProductAttributeService;
import com.itheima.service.IProductCategoryAttributeRelationService;
import com.itheima.service.IProductCategoryService;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.ProductCategoryVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 产品分类 服务实现类
 * </p>
 *
 * @author fukeke
 * @since 2022-11-11
 */
@DubboService       //创建对象
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategory> implements IProductCategoryService {

    @Autowired
    ProductCategoryMapper productCategoryMapper;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Override
    public PageBeanVo findByParentIdAndPage(Long parentId,Integer pageNum,Integer pageSize)  {
        //添加查询条件
        LambdaQueryWrapper<ProductCategory> qw=new LambdaQueryWrapper<>();
        qw.eq(ProductCategory::getParentId,parentId);

        //添加分页条件
        Page<ProductCategory> page=new Page<>(pageNum,pageSize);

        productCategoryMapper.selectPage(page,qw);

        return new PageBeanVo(pageNum,pageSize,page.getTotal(),page.getRecords());
    }

    @Override
    public ProductCategory saveProductCategory(ProductCategory productCategory) {
        String url = stringRedisTemplate.opsForValue().get("url");
        productCategory.setIcon(url);
        stringRedisTemplate.delete("url");
        if(productCategory.getParentId()==0){
            productCategory.setLevel(0);
        }else{
            productCategory.setLevel(1);
        }
        productCategoryMapper.insert(productCategory);
        return productCategory;
    }

    @Override
    public void deleteProductCategory(Long id) {
        productCategoryMapper.deleteById(id);
    }

    @Override
    public List<ProductCategoryVo> findTreeCategory() {

        //查询一级分类（level=0）
        LambdaQueryWrapper<ProductCategory> qw=new LambdaQueryWrapper<>();
        qw.eq(ProductCategory::getLevel,0);
        List<ProductCategory> productCategories = productCategoryMapper.selectList(qw);

        //遍历一级分类，将其封装为ProductCategoryVo对象，并查询子分类
        List<ProductCategoryVo> voList=new ArrayList<>();
        productCategories.forEach(productCategory -> {
            ProductCategoryVo productCategoryVo=new ProductCategoryVo();
            BeanUtil.copyProperties(productCategory,productCategoryVo);

            //获取一级分类的id，根据其去查询二级分类
            Long id = productCategory.getId();
            LambdaQueryWrapper<ProductCategory> qw2=new LambdaQueryWrapper<>();
            qw2.eq(ProductCategory::getParentId,id);
            List<ProductCategory> productCategories1 = productCategoryMapper.selectList(qw2);
            productCategoryVo.setChildren(productCategories1);
            voList.add(productCategoryVo);
        });
        return voList;
    }

    @Override
    public ProductCategory findById(Long categoryId) {
        return productCategoryMapper.selectById(categoryId);
    }

    @Override
    public void updateCategory(ProductCategory productCategory) {
        if(stringRedisTemplate.hasKey("url")){
            String url = stringRedisTemplate.opsForValue().get("url");
            productCategory.setIcon(url);
            stringRedisTemplate.delete("url");
        }
        productCategoryMapper.updateById(productCategory);
    }
}
